<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Files_model extends Mongo_model {
	
    protected $_table_name = 'files';
	
	
	public function __construct()
    {
        parent::__construct();
    }
	
	
	public function insert_files($data = null)
	{
		if(empty($data))
		{
			return false;
		}
		$data['ctime'] = time();
		//$data['mtime'] = 0;
		$data['dtime'] = 0;
		
		$id = $this->get_no();

		$data['id'] = $id;
		$data['no'] = $id . "." . ltrim( $data["ext"],".");
		
		if($this->insert($data) == NULL) {
			return NULL;
		}
		unset($data["data"]);
		return $data;
	}

	public function get_info_files($file = NULL)
	{
		if($file == NULL)
		{
			return false;
		}

		$this->where('no', $file);
		$this->where('dtime', 0);
		return $this->get_once();
	}
	
	public function delect_files($id)
	{
		if(empty($id))
		{
			return false;
		}
		$this->where('id', $id);
		
		return $this->update('dtime',time() );
	}
	
	public function get_files_lists($val=NULL,$key = "cid")
	{

		$data_info['dtime'] = 0;
		$data_info[$key] = $val;

		$this->where($data_info);

		$select_arr = array("file","ext","id","file_name","file_type","file_ext","file_size","no","ctime");
		$this->select($select_arr);
		$this->order_by(array('ctime' => 'DESC'));

		$this->limit(20);

		return $this->get();;
	}
}